package com.sisgesbyte.dao.ventas;

import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.sisgesbyte.dao.beanDao.ClienteLocalesRequest;
import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.ventas.LocalesClientesEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantLocalesClienteDaoImpl extends AbstractFacade<LocalesClientesEntity> implements MantLocalesClienteDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;
	
	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantLocalesClienteDaoImpl(){
		super(LocalesClientesEntity.class);
	}
	
	@Override
	public List<LocalesClientesEntity> listaLocales(FiltrosSQLBean cadenaFiltros) {
		
		StringBuilder jpql = new StringBuilder();
		jpql.append("select cl.id id_local, cl.cod_local,cl.cliente_id ,cl.comun_objeto_id, ");
		jpql.append("p.id id_pais,p.nombre pais,dp.id id_departamento, dp.nombre departamento, ");
		jpql.append("ci.id id_ciudad, ci.nombre ciudad,di.id id_distrito, ");
		jpql.append("di.nombre distrito,co.direccion, ");
		jpql.append("co.telefono1,co.email,cl.principal,cl.active ");
		jpql.append("from ventas.cliente_locales cl  ");
		jpql.append("left join comun.comun_objeto co on co.id=cl.comun_objeto_id ");
		jpql.append("left join localizacion.pais p on co.pais_id=p.id ");
		jpql.append("left join localizacion.division_pais dp on co.division_pais_id=dp.id ");
		jpql.append("left join localizacion.ciudad ci on co.ciudad_id=ci.id ");
		jpql.append("left join localizacion.distrito di on co.distrito_id=di.id ");
		
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(), LocalesClientesEntity.class);
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<LocalesClientesEntity> ls = query.getResultList();

		return ls;
	}
	
	@Override
	public GenericEntity nuevoLocalCliente(ClienteLocalesRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from ventas.sp_registrar_local(:in_id_cliente, :in_comun_objeto_id, ");
		jpql.append(":in_principal_local, :in_active_local) ");
		
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_id_cliente", request.getIdCliente());
			query.setParameter("in_comun_objeto_id", request.getIdObjetoComun());
			query.setParameter("in_principal_local", request.getPrincipalLocal());
			query.setParameter("in_active_local", request.getActiveLocal());
			
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarLocalesCliente( ClienteLocalesRequest request) {
		
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from ventas.sp_actualizar_local(:in_id_local, :in_principal_local, ");
		jpql.append(":in_active_local ) ");
		
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_id_local", request.getIdLocal());
			query.setParameter("in_principal_local", request.getPrincipalLocal());
			query.setParameter("in_active_local", request.getActiveLocal());
			
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarLocalesCliente(int codLocal) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from ventas.sp_eliminar_local(:in_id_local)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_id_local", codLocal);

		return (GenericEntity) query.getSingleResult();
	}

}
